﻿namespace EFMLEditor.Internal
{
    using System;
    using System.IO;

    public class StringStream : Stream
    {
        private readonly string _str;

        private readonly long _byteLength;

        private int _position;

        public StringStream(string str)
        {
            if (str == null)
            {
                throw new ArgumentNullException("str");
            }
            this._str = str;
            this._byteLength = this._str.Length * 2;
            this._position = 0;
        }

        public override bool CanRead
        {
            get
            {
                return true;
            }
        }

        public override bool CanSeek
        {
            get
            {
                return true;
            }
        }

        public override bool CanWrite
        {
            get
            {
                return false;
            }
        }

        public override long Length
        {
            get
            {
                return this._byteLength;
            }
        }

        public override long Position
        {
            get
            {
                return this._position;
            }
            set
            {
                if (value < 0 || value > int.MaxValue)
                {
                    throw new ArgumentOutOfRangeException("Position");
                }
                this._position = (int)value;
            }
        }

        public override long Seek(long offset, SeekOrigin origin)
        {
            switch (origin)
            {
                case SeekOrigin.Begin:
                    this.Position = offset;
                    break;
                case SeekOrigin.End:
                    this.Position = this._byteLength + offset;
                    break;
                case SeekOrigin.Current:
                    this.Position = this.Position + offset;
                    break;
            }
            return this.Position;
        }

        public override int Read(byte[] buffer, int offset, int count)
        {
            if (this._position < 0)
            {
                throw new InvalidOperationException();
            }
            int bytesRead = 0;
            while (bytesRead < count)
            {
                if (this._position >= this._byteLength)
                {
                    return bytesRead;
                }
                char c = this._str[this._position / 2];
                buffer[offset + bytesRead] = (byte)((this._position % 2 == 0) ? c & 0xFF : (c >> 8) & 0xFF);
                this.Position++;
                bytesRead++;
            }
            return bytesRead;
        }

        public override void Write(byte[] buffer, int offset, int count)
        {
            throw new NotSupportedException();
        }

        public override void Flush()
        {
            throw new NotSupportedException();
        }

        public override void SetLength(long value)
        {
            throw new NotSupportedException();
        }
    }
}